#include "client.h"

int isValidCommand(const char* cmd){
    if(strcmp("ls",cmd)==0){
        return 1;
    }
    else if(strcmp("cd",cmd)==0){
        return 1;
    }
    else if(strcmp("pwd",cmd)==0){
        return 1;
    }
    else if(strcmp("mkdir",cmd)==0){
        return 1;
    }
    else if(strcmp("rmdir",cmd)==0){
        return 1;
    }
    else if(strcmp("puts",cmd)==0){
        return 1;
    }
    else if(strcmp("gets",cmd)==0){
        return 1;
    }
    else if(strcmp("remove",cmd)==0){
        return 1;
    }
    return 0;
}

int getCommandType(const char *cmd,commandPacket_t* pcommandPacket){
    if(strcmp("ls",cmd)==0){
        pcommandPacket->cmd=LSCOMMAND;
    }
    else if(strcmp("cd",cmd)==0){
        pcommandPacket->cmd=CDCOMMAND;
    }
    else if(strcmp("pwd",cmd)==0){
        pcommandPacket->cmd=PWDCOMMAND;
    }
    else if(strcmp("mkdir",cmd)==0){
        pcommandPacket->cmd=MKDIRCOMMAND;
    }
    else if(strcmp("rmdir",cmd)==0){
        pcommandPacket->cmd=RMDIRCOMMAND;
    }
    else if(strcmp("puts",cmd)==0){
        pcommandPacket->cmd=PUTSCOMMAND;
    }
    else if(strcmp("gets",cmd)==0){
        pcommandPacket->cmd=GETSCOMMAND;
    }
    else if(strcmp("remove",cmd)==0){
        pcommandPacket->cmd=REMOVECOMMAND;
    }
    return 0;

}

int recvn(int sockfd,void *buf,int length){
    ssize_t cur_count=0;
    char *p=(char *)buf;
    while(cur_count<length){
        ssize_t sret=recv(sockfd,p+cur_count,length-cur_count,0);
        cur_count+=sret;
    }
    return 0;
}

int setCommandPacket(commandPacket_t* pcommandPacket,const char *cmd,const char *arg){
    getCommandType(cmd,pcommandPacket);                                              
    if(arg==NULL)
    {
        pcommandPacket->length=0;
    }
    else
    {
        pcommandPacket->length=strlen(arg);
    }
    if(arg!=NULL)
        strcpy(pcommandPacket->argument,arg);
    else
    {
        strcpy(pcommandPacket->argument,".");
    }
    return 0;
}
